package main

import (
	"time"
	"fmt"
	"math/rand"
	"runtime"
)

func task(id int) int {
	time.Sleep(time.Millisecond*100*time.Duration(rand.Int63n(10)))
	return id*100
}

func main() {
	ch:=make(chan int)
	cancel:=make(chan bool)

	for i:=0; i<10; i++ {
		go func(i int){
			r:=task(i)
			select {
				case ch<-r:
					close(cancel)
					break
				case <-cancel:
					fmt.Println("canceled")
					break

			}
		}(i)
	}

	fmt.Println("Num of Goroutine", runtime.NumGoroutine())
	result:=<-ch
	fmt.Println("result",result)
	time.Sleep(time.Millisecond*2000)
	fmt.Println("Num of Goroutine", runtime.NumGoroutine())
}
